// pages/onAndOfLine/onAndOfLine.js
const app = getApp()
const util = require('../../utils/util.js')
const lbbService = require('../service/service.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabNum: '1',
        equScrollTop: 0,
        equip_ver: '',
        boxTypeIndex: 0, //控制盒类型：长钩/短钩
        pileVerIndex: 0, //v3.0/v3.1
        isLocationShow: false,
        locationArray: ['门诊', '急诊', '住院部'],
        locationDataArray: [],
        type_animationData: {},
        typelistnum:3,
        locationIndex: -1,
        operatedPileListInfo: [], //操作后的桩列表
        operatedChairListInfo: [], //操作后的轮椅列表
        operatedCarListInfo: [], //操作后的担架列表
        equipType: 0, //桩类型：1轮椅，2担架
        equipTypes: [{
            value: '1',
            name: '轮椅'
        }, {
            value: '2',
            name: '担架'
        }],
        boxType: 'V3.0短钩', //string
        equ_lock_types: [{ //设备锁 长钩 短钩
            value: '长钩',
        }, {
            value: '短钩',
        }, {
            value: '软链锁扣',
        }, {
            value: '直行锁短钩',
        }],
        equ_lock_type: '', //string
        onlinePileInfo: [],
        onlineCarInfo: [],
        onlineChairInfo: [],
        pileOffType: '更换控制盒',
        equipOffType: 'rfid卡损坏下线',
        pileCodeStr: '',
        pileInfoList: [],
        inputPileBlank: false,
        gatewayTypeBtns: [{
            name: '4G网关',
            isActive: true,
            type: '4G',
            categoryId: 25
        }, {
            name: 'NB网关',
            isActive: false,
            type: 'NB',
            categoryId: 18
        }],
        gatewayHid: '',
        gatewayInfoList: null,
        gatewayCategoryId: util.CONST_DATA.GATEWAY_CATEGORY_ID,
        //选择绑定的位置信息
        gatewaySelBindPosition: [],
        //选择绑定的设备信息
        gatewaySelBindEqu: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getEquVer()
        let that = this

        lbbService.getBaseConfig({}, res => {
            app.globalData.config = res;
            that.setData({
                scrollHeight: app.getScrollHeight(103),
                equipTypes: app.globalData.config.typeList,
                lockTypes: app.globalData.config.lockType,
            })
        })
        let equipOffTypes = []
        let pileOffTypes = []

        lbbService.getEquipmentOffReason({}, function (res) {
            Object.keys(res.data).forEach(function (key) {
                let checked = false;
                if (key == 1) {
                    checked = true;
                    that.data.equipOffType = res.data[key];

                }
                equipOffTypes.push({
                    "value": key,
                    "name": res.data[key],
                    "checked": checked
                });
            })
            equipOffTypes.push({
                name: '其他',
                value: 0,
                checked: false
            })
            that.setData({
                equipOffTypes: equipOffTypes
            })

        })

        lbbService.getEquOfflineReason({}, function (res) {
            Object.keys(res.data).forEach(function (key) {
                let checked = false
                if (key == 1) {
                    checked = true
                    that.data.pileOffType = res.data[key]
                }
                pileOffTypes.push({
                    name: res.data[key],
                    value: key,
                    checked: checked
                })
            })
            pileOffTypes.push({
                name: '其他',
                value: 0,
                checked: false
            })
            that.setData({
                pileOffTypes: pileOffTypes
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        let that = this
        that.toast = that.selectComponent("#toast");
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let that = this
        if (!!that.data.changedLocationInfo) {
            that.getLocationList()
            if (that.data.tabNum == 5) {
                that.getHosGatewayEquShip()
            }
        }
        if (that.data.inputPileBlank && that.data.pileCodeStr) {
            that.data.inputPileBlank = false;
            that.getPileListInfo();
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },
    /**
     * 控制盒类型变化：长钩/短钩
     */
    boxTypeChange: function (e) {
        util.print('boxTypeChange', e);
        let that = this;
        let boxTypeIndex = e.detail.value;
        let pileIndex = e.currentTarget.dataset.pileIndex;
        let pileInfoList = that.data.pileInfoList;
        pileInfoList[pileIndex].hpLockType = pileInfoList[pileIndex].pileVer[boxTypeIndex];
        that.setData({
            pileInfoList: pileInfoList,
        })
    },
    /**
     * 控制盒版本变化：v3.0/v3.1
     */
    pileVerChange: function (e) {
        util.print('pileVerChange', e);
        let that = this;
        let pileVerIndex = e.detail.value;
        let pileIndex = e.currentTarget.dataset.pileIndex;
        let pileInfoList = that.data.pileInfoList;
        pileInfoList[pileIndex].hpVerVal = pileInfoList[pileIndex].verList[pileVerIndex];
        that.setData({
            pileInfoList: pileInfoList,
        })
    },
    /**
     * 删除某一控制盒
     */
    deletePile: function (e) {
        let that = this;
        let pileIndex = e.currentTarget.dataset.pileIndex;
        let pileCode = that.data.pileInfoList[pileIndex].pileCode;
        let strIndex = that.data.pileCodeArr.findIndex(item => item == pileCode);
        util.print('pileIndex', pileIndex, pileCode, strIndex);
        that.data.pileInfoList.splice(pileIndex, 1);
        that.data.pileCodeArr.splice(strIndex, 1);
        that.setData({
            pileInfoList: that.data.pileInfoList,
            pileCodeArr: that.data.pileCodeArr,
        })

    },
    offPileTypeChange: function (e) {
        let pileOffType = e.detail.value
        this.setData({
            pileOffType
        })
    },
    offEquipTypeChange: function (e) {
        let equipOffType = e.detail.value
        this.setData({
            equipOffType
        })
    },
    equVerChange: function (e) {
        let that = this
        console.log(e)
        let equip_ver = e.detail.value
        that.setData({
            equip_ver
        })
    },
    /**
     * 设备锁类型变化：长钩/短钩
     */
    equLockTypeChange: function (e) {
        let that = this
        let equ_lock_type = e.detail.value
        that.setData({
            equ_lock_type: equ_lock_type
        })
    },
    inputPileDesc: function (e) {
        let that = this
        let desc = e.detail.value
        that.setData({
            pileOffType: desc
        })

    },
    inputEquipDesc: function (e) {
        let desc = e.detail.value
        this.setData({
            equipOffType: desc
        })
    },
    chooseHospital: function () {
        wx.navigateTo({
            url: '/pages/chooseHospital/chooseHospital',
        })
    },
    getEquVer: function () {
        let that = this
        let equ_vers = []
        lbbService.getEquVer({}, function (res) {
            that.setData({
                inputChairIsHide: res.inputChairIsHide
            })
            Object.keys(res.data).forEach(function (key) {
                let checked = false;
                // if (res.data[key]=='G25-2'){
                //   checked = true
                // }
                equ_vers.push({
                    value: res.data[key],
                    checked: checked
                })
            })
            that.setData({
                equ_vers: equ_vers
            })
        })
    },
    getHosGatewayEquShip: function () { // mark: 获取医院的网关设备信息
        let that = this
        lbbService.getHosGatewayEquShip({
            hosId: parseInt(that.data.changedLocationInfo.hospitalId),
            gatewayMac: that.data.gatewayMac
        }, function (res) {
            let hosEquArr = new Array()
            let equMap = {}
            if (res.data) {
                res.data.forEach(el => {
                    let position = el.position
                    let arr = equMap[position]
                    if (!arr) {
                        arr = new Array()
                        equMap[position] = arr
                    }
                    arr.push(el)
                })
                for (let key in equMap) {
                    let val = equMap[key]
                    let equArr = new Array()
                    for (let i = 0; i < val.length; i += 2) {
                        //位置是否绑定                        
                        let vArr = new Array()
                        vArr.push(val[i])
                        let v2 = val[i + 1]
                        if (v2) {
                            vArr.push(v2)
                        }
                        equArr.push(vArr)
                    }

                    equArr.forEach(eArr => {
                        eArr.forEach(e => {
                            if (e.checked) {
                                that.data.gatewaySelBindEqu.push(e.hid)
                                that.data.gatewaySelBindPosition.push(key)
                                let lIndex = that.data.locationArray.indexOf(key)
                                that.data.locationDataArray[lIndex].isCheck = true
                            }
                        })
                    })
                    let item = {
                        position: key,
                        equArr: equArr
                    }
                    hosEquArr.push(item)
                }
            }
            that.setData({
                hosEquArr: hosEquArr,
                locationDataArray: that.data.locationDataArray
            })
        })
    },
    getLocationList: function () { // mark: 获取医院位置信息
        let that = this
        lbbService.getPosByHos({
            hospitalId: parseInt(that.data.changedLocationInfo.hospitalId)
        }, function (res) {
            that.setData({
                locationArray: res.data
            })
            that.setData({
                locationIndex: that.data.locationArray.length - 1
            })
            if (that.data.tabNum == 5) {
                let locationDataArray = new Array()
                that.data.locationArray.forEach((value, index) => {
                    let laObj = {
                        location: value,
                        isCheck: false
                    }
                    locationDataArray.push(laObj)
                })
                that.setData({
                    locationDataArray: locationDataArray
                })
            }
        })
    },
    //选择位置
    chooseLocation: function (e) {
        let that = this
        this.setData({
            locationIndex: e.currentTarget.dataset.location,
            isLocationShow: false
        })
        if (this.data.tabNum == 5) {
            //mark: 网关选择位置
            let selPosition = that.data.locationArray[that.data.locationIndex]
            let isExistsPositon = that.data.gatewaySelBindPosition.indexOf(selPosition)
            if (isExistsPositon == -1) {
                that.data.gatewaySelBindPosition.push(selPosition)
                that.data.locationDataArray[that.data.locationIndex].isCheck = true
                that.setData({
                    gatewaySelBindPosition: that.data.gatewaySelBindPosition,
                    locationDataArray: that.data.locationDataArray
                })
            }
            let gatewaySelBindEqu = new Array()
            that.data.hosEquArr.forEach(equ => {
                if (selPosition && equ.position == selPosition) {
                    let arr = equ.equArr
                    arr.forEach(i => {
                        i.forEach(b => {
                            b.checked = true
                            gatewaySelBindEqu.push(b.hid)
                        })
                    })
                }
            })
            that.data.gatewaySelBindEqu = that.data.gatewaySelBindEqu.concat(gatewaySelBindEqu)
            console.log("selEquList===>", that.data.gatewaySelBindEqu)
            that.setData({
                hosEquArr: that.data.hosEquArr,
            })
        }
    },
    //新增位置
    addLocation: function (e) {
        console.log(e)
        let that = this
        let choosedlocation = e.detail.value.location
        if (!that.data.changedLocationInfo) {
            if (choosedlocation != '') {
                that.data.locationArray.push(choosedlocation)
                let locationArray = that.data.locationArray
                that.setData({
                    locationArray: locationArray,
                    locationIndex: locationArray.length - 1,
                    isLocationShow: false
                })
            }
        } else {
            lbbService.addPosByHos({
                hospitalId: parseInt(that.data.changedLocationInfo.hospitalId),
                position: choosedlocation
            }, function (res) {
                that.getLocationList()
                that.hideLocationModal()
            })
        }
    },
    equipTypeChange: function (e) {
        console.log(e)
        this.setData({
            equipType: e.detail.value
        })
    },
    lockTypeChange(e) {
        this.setData({
            lockType: e.detail.value
        })
    },
    // boxTypeChange: function(e) {
    //   console.log(e)
    //   this.setData({
    //     boxType: e.detail.value
    //   })
    // },
    toggleTab: function (e) {
        console.log(e)
        let tabNum = e.currentTarget.dataset.num
        this.setData({
            tabNum: tabNum
        })
    },
    showLocationModal: function () {
        let that = this
        that.setData({
            isLocationShow: true
        })
    },
    hideLocationModal: function () {
        this.setData({
            isLocationShow: false
        })
    },
    stopMaoPao: function () {
        return
    },
    /**
     * 输入控制盒编号
     */
    inputPileCode: function (e) {
        let that = this;
        let pileCodeStr = e.detail.value;
        that.setData({
            pileCodeStr: pileCodeStr,
        })
    },
    /**
     * 获取控制盒信息列表
     */
    getPileListInfo: function () {
        let that = this;
        let reg1 = /[^0-9]+/;
        let reg2 = /^[^0-9]+|[^0-9]+$/g; //对非数字去头去尾
        let pileCodeStr = that.data.tabNum == 5 ? that.data.gatewayHid : that.data.pileCodeStr.replace(reg2, '');
        console.log("getPileListInfo--->", pileCodeStr)
        let pileCodeArr = pileCodeStr.toString().split(reg1);
        that.setData({
            pileCodeStr: pileCodeStr,
            pileCodeArr: pileCodeArr
        })

        if (pileCodeStr) {
            that.data.inputPileBlank = false;
            lbbService.getPileBatch({
                pileCode: pileCodeStr,
                tabNum: that.data.tabNum
            }, function (res) {
                if (that.data.tabNum == 5) {
                    that.handleGatewayData(res.data)
                } else {
                    that.setData({
                        pileInfoList: res.data
                    })
                }
            })
        } else {
            that.data.inputPileBlank = true;
        }
    },
    // mark: 获取网关设备信息
    handleGatewayData: function (data) {
        let that = this
        if (!data || data.length == 0) {
            wx.showToast({
                title: '网关不存在',
            })
        } else {
            let gatewayInfo = data[0]
            that.data.gatewayMac = gatewayInfo.mac
            that.data.gatewayTypeBtns.forEach(el => {
                if (el.type == '4G' && gatewayInfo.categoryId == that.data.gatewayCategoryId.CATEGORY_4G) {
                    el.isActive = true
                } else if (el.type == 'NB' && gatewayInfo.categoryId == that.data.gatewayCategoryId.CATEGORY_NB) {
                    el.isActive = true
                } else {
                    el.isActive = false
                }
            })

            let locationArray = new Array()
            let locationDataArray = new Array()
            let gatewaySelBindEqu = new Array()
            let gatewaySelBindPosition = new Array()
            let locationIndex = -1
            let hosEquArr = new Array()
            let changedLocationInfo = {
                hospitalName: gatewayInfo.pileHospital,
                hospitalId: gatewayInfo.hospitalId
            }
            if (gatewayInfo.positionArrStr) {
                locationArray = gatewayInfo.positionArrStr.split(',')
                locationArray.forEach((value, index) => {
                    let laObj = {
                        location: value,
                        isCheck: false
                    }
                    locationDataArray.push(laObj)
                    if (gatewayInfo.location == value) {
                        locationIndex = index
                    }
                })
            }
            let equMap = {}
            if (gatewayInfo.hosEquInfoDtoList) {
                gatewayInfo.hosEquInfoDtoList.forEach(el => {
                    let position = el.position
                    let arr = equMap[position]
                    if (!arr) {
                        arr = new Array()
                        equMap[position] = arr
                    }
                    arr.push(el)
                })
                for (let key in equMap) {
                    let val = equMap[key]
                    let equArr = new Array()
                    for (let i = 0; i < val.length; i += 2) {
                        //位置是否绑定                        
                        let vArr = new Array()
                        vArr.push(val[i])
                        let v2 = val[i + 1]
                        if (v2) {
                            vArr.push(v2)
                        }
                        equArr.push(vArr)
                    }

                    equArr.forEach(eArr => {
                        eArr.forEach(e => {
                            if (e.checked) {
                                gatewaySelBindEqu.push(e.hid)
                                gatewaySelBindPosition.push(key)
                                let lIndex = locationArray.indexOf(key)
                                locationDataArray[lIndex].isCheck = true
                            }
                        })
                    })
                    let item = {
                        position: key,
                        equArr: equArr
                    }
                    hosEquArr.push(item)
                }
            }
            that.data.gatewaySelBindPosition = gatewaySelBindPosition
            that.data.gatewaySelBindEqu = gatewaySelBindEqu
            if (gatewayInfo.categoryId == that.data.gatewayCategoryId.CATEGORY_NB || gatewayInfo.categoryId == that.data.gatewayCategoryId.CATEGORY_4G) {} else {
                wx.showToast({
                    title: '非网关',
                })
            }
            //默认选择的位置信息
            that.data.gatewaySelBindPosition = Array.from(new Set(that.data.gatewaySelBindPosition))
            that.data.gatewaySelBindEqu = Array.from(new Set(that.data.gatewaySelBindEqu))
            console.log("locationDataArray", locationDataArray, that.data.gatewaySelBindPosition)

            that.setData({
                gatewayTypeBtns: that.data.gatewayTypeBtns,
                changedLocationInfo: changedLocationInfo,
                locationArray: locationArray,
                locationIndex: locationIndex,
                hosEquArr: hosEquArr,
                locationDataArray: locationDataArray
            })
        }
    },
    /**
     * 输入轮椅/担架编号
     */
    inputEquipCode: function (e) {
        let that = this
        let equipCodeStr = e.detail.value;
        if (that.data.tabNum == 3 || that.data.tabNum == 4) {
            that.setData({
                carCodeStr: equipCodeStr
            })
        } else if (that.data.tabNum == 5) {
            //网关编号
            that.setData({
                gatewayHid: equipCodeStr
            })
        } else {
            that.setData({
                chairCodeStr: equipCodeStr
            })
        }

    },
    /**
     * 获取轮椅/担架信息列表
     */
    getEquipListInfo: function () {
        let that = this
        let equipCodeStr = that.data.chairCodeStr;
        if (that.data.tabNum == 3 || that.data.tabNum == 4) {
            equipCodeStr = that.data.carCodeStr;
        }

        console.log(equipCodeStr, '执行到这儿了')
        if (equipCodeStr) {
            lbbService.getEquBatch({
                equCode: equipCodeStr
            }, function (res) {
                console.log("获取输入的设备信息:", that.data.tabNum, res)
                if (that.data.tabNum == 3 || that.data.tabNum == 4) {
                    that.setData({
                        carInfoList: res.data
                    })
                } else {
                    that.setData({
                        chairInfoList: res.data
                    })
                }
            })
        }
    },
    /**
     * 点击上线按钮
     */
    onLine: function () {
        let that = this;
        let tabNum = that.data.tabNum;
        if (tabNum == 1) {
            that.onLinePile();
        } else if (tabNum == 2) {
            that.onLineEqu();
        } else if (tabNum == 3) {
            that.onLineCar();
        } else if (tabNum == 4) {
            that.onLineEquipment();
        } else if (tabNum == 5) {
            that.onLineGateway()
        }
    },
    /**
     * 点击下线按钮
     */
    offLine: function () {
        let that = this;
        let tabNum = that.data.tabNum;
        if (tabNum == 1 || tabNum == 5) {
            that.showModal1();
        } else if (tabNum == 2) {
            that.showModal2();
        } else if (tabNum == 3 || tabNum == 4) {
            that.showModal3();
        }
    },
    /**
     * 上线网关并建立绑定关系，清除之前的关系
     */
    onLineGateway: function () {
        let that = this
        let gatewayLockType = '4G网关'
        let categoryId = 25
        that.data.gatewayTypeBtns.forEach(el => {
            if (el.isActive) {
                gatewayLockType = el.name
                categoryId = el.categoryId
            }
        })

        let selBindEquArr = new Array()
        that.data.hosEquArr.forEach(equ => {
            let arr = equ.equArr
            arr.forEach(i => {
                i.forEach(b => {
                    if (b.checked) {
                        selBindEquArr.push(b.hid)
                    }
                })
            })
        })
        that.data.gatewaySelBindEqu = selBindEquArr
        console.log("上线网关建立-->", that.data.gatewaySelBindPosition, that.data.gatewaySelBindEqu, that.data.gatewayHid)
        let pileInfo = new Array()
        pileInfo.push({
            pileCode: that.data.gatewayHid,
            hpLockType: gatewayLockType,
            hpVerVal: 'V1.1'
        })
        lbbService.addPileBatch({
            hospitalId: parseInt(that.data.changedLocationInfo.hospitalId),
            pileInfo: JSON.stringify(pileInfo),
            pileLocation: that.data.locationArray[that.data.locationIndex],
            equipType: categoryId,
            poleStatus: 1
        }, function (res) {
            if (res.result == 0) {
                lbbService.onlineGateway({
                    gatewayMac: that.data.gatewayMac,
                    equIds: that.data.gatewaySelBindEqu
                }, function (res) {
                    that.toast.showToast('录入成功', 'success');
                })
            } else {
                that.toast.showToast(res.description, 'warning', 3000)
            }
        })

    },
    /**
     * 选择要绑定的控制盒触发的操作 // mark: 网关绑定控制盒
     * @param {} e 
     */
    selGatewayBindEqu: function (e) {
        console.log("绑定控制盒触发操作:", e, e.detail)
        this.data.gatewaySelBindEqu = e.detail.value

        //检查位置下设备全部取消或选中后位置
        let that = this
        let chePositionCount = {}
        that.data.hosEquArr.forEach(equ => {
            let arr = equ.equArr
            chePositionCount[equ.position] = 0
            arr.forEach(i => {
                i.forEach(b => {
                    let hid = b.hid
                    let hidIsExists = that.data.gatewaySelBindEqu.indexOf(hid.toString())
                    b.checked = hidIsExists >= 0
                    if (b.checked) {
                        chePositionCount[equ.position]++
                    }
                })
            })
        })
        let selPositionArr = new Array()
        that.data.locationDataArray.forEach(p => {
            p.isCheck = chePositionCount[p.location] > 0
            if (p.isCheck) {
                selPositionArr.push(p.location)
            }
        })
        that.data.gatewaySelBindPosition = Array.from(new Set(selPositionArr))
        that.setData({
            locationDataArray: that.data.locationDataArray,
            hosEquArr: that.data.hosEquArr
        })
    },
    /**
     * 选择绑定的位置信息 // mark: 网关绑定位置
     */
    selBindEquPosition: function (e) {
        console.log("绑定位置触发操作:", e, e.detail.value, this.data.gatewaySelBindPosition)
        let that = this
        let selPositionArr = e.detail.value
        let selAddPosition = selPositionArr.filter(item => !that.data.gatewaySelBindPosition.includes(item))
        let selSubPosition = that.data.gatewaySelBindPosition.filter(item => !selPositionArr.includes(item))
        console.log("---->", that.data.gatewaySelBindPosition, selAddPosition, selSubPosition)
        let selPosition = ''
        let isAdd = false
        if (selAddPosition && selAddPosition.length > 0) {
            //增加位置
            selPosition = selAddPosition[0]
            isAdd = true
        } else if (selSubPosition && selSubPosition.length > 0) {
            //减少位置
            selPosition = selSubPosition[0]
        } else {

        }
        console.log("选择的位置：", selPosition, isAdd)
        that.data.hosEquArr.forEach(equ => {
            if (selPosition && equ.position == selPosition) {
                let arr = equ.equArr
                arr.forEach(i => {
                    i.forEach(b => {
                        b.checked = isAdd
                    })
                })
            }
        })
        that.data.gatewaySelBindPosition = selPositionArr
        that.setData({
            hosEquArr: that.data.hosEquArr
        })
    },
    /**
     * 上线桩位
     */
    onLinePile: function () {
        var that = this;
        if (!that.data.pileCodeStr) {
            that.toast.showToast('请输入编号');
            return
        }
        if (!that.data.changedLocationInfo) {
            that.toast.showToast('请选择医院');
            return
        }
        if (that.data.locationIndex < 0) {
            that.toast.showToast('请选择位置');
            return
        }
        if (that.data.equipType == 0) {
            that.toast.showToast('请选择桩类型');
            return
        }
        let pileInfo = [];
        let pileInfoList = that.data.pileInfoList;
        console.log(pileInfoList, '找到这个pileInfoList')
        let found = pileInfoList.find(item => {
            return !item.hpLockType || !item.hpVerVal
        });
        util.print('found', found);
        if (found) {
            that.toast.showToast('请选择Z' + found.pileCode + '类型/版本');
            return
        }
        pileInfoList.forEach(item => {
            pileInfo.push({
                pileCode: item.pileCode,
                hpLockType: item.hpLockType,
                hpVerVal: item.hpVerVal
            });
        })
        lbbService.addPileBatch({
            hospitalId: parseInt(that.data.changedLocationInfo.hospitalId),
            pileInfo: JSON.stringify(pileInfo),
            pileLocation: that.data.locationArray[that.data.locationIndex],
            // equipCode: 0,
            equipType: that.data.equipType,
            poleStatus: 1
        }, function (res) {
            if (res.result == 0) {
                that.toast.showToast('录入成功', 'success');
                for (let i = 0; i < that.data.equipTypes.length; i++) {
                    if (that.data.equipTypes[i].value == that.data.equipType) {
                        that.setData({
                            deviceName: that.data.equipTypes[i].name
                        })
                        console.log(that.data.deviceName, 'deviceName')
                    }
                }
                that.pushPile(that.data.changedLocationInfo.hospitalName, that.data.pileInfoList, '上线', that.data.deviceName, that.data.locationArray[that.data.locationIndex])
                that.getEquipListInfo()

            } else {
                that.toast.showToast(res.description, 'warning', 3000)
            }
        })
    },
    //上线担架
    onLineCar: function () {
        var that = this;
        if (!that.data.carCodeStr) {
            that.toast.showToast('请输入编号');
            return
        }
        if (!that.data.changedLocationInfo) {
            that.toast.showToast('请选择医院');
            return
        }
        lbbService.addEquBatch({
            hospitalId: parseInt(that.data.changedLocationInfo.hospitalId),
            equipCode: that.data.carCodeStr,
            equipType: 2,
        }, function (res) {
            that.toast.showToast('新增成功', 'success');
            that.pushEquip(that.data.carCodeStr, '上线')
            that.getEquipListInfo()

        })
    },
    //上架设备
    onLineEquipment() {
        var that = this;
        if (!that.data.carCodeStr) {
            that.toast.showToast('请输入设备编号');
            return
        }
        if (!that.data.lockType) {
            that.toast.showToast('请输入锁钩类型');
            return
        }
        if (!that.data.changedLocationInfo) {
            that.toast.showToast('请选择医院');
            return
        }
        lbbService.addEquBatch({
            hospitalId: parseInt(that.data.changedLocationInfo.hospitalId),
            equipCode: that.data.carCodeStr,
            equipType: that.data.equipType,
            eqtLockType: that.data.lockType
        }, function (res) {
            that.toast.showToast('新增成功', 'success');
            that.pushEquip(that.data.carCodeStr, '上线')
            that.getEquipListInfo()

        })
    },
    //上线轮椅
    onLineEqu: function () {
        var that = this;
        if (!that.data.chairCodeStr) {
            that.toast.showToast('请输入编号');
            return
        }
        if (!that.data.changedLocationInfo) {
            that.toast.showToast('请选择医院');
            return
        }
        if (!that.data.equ_lock_type) {
            that.toast.showToast('请选择设备类型');
            return
        }
        if (!that.data.equip_ver) {
            that.toast.showToast('请选择型号');
            return
        }
        lbbService.addEquBatch({
            hospitalId: parseInt(that.data.changedLocationInfo.hospitalId),
            equipCode: that.data.chairCodeStr,
            eqtModelName: that.data.equip_ver,
            eqtLockType: that.data.equ_lock_type,
            equipType: 1,
        }, function (res) {
            that.toast.showToast('新增成功', 'success');
            that.pushEquip(that.data.chairCodeStr, '上线')
            that.getEquipListInfo()

        })
    },
    hideModal1: function () {
        let that = this
        that.setData({
            isShow_Modal1: false,
            pileOffType: ''
        })
    },
    showModal1: function () {
        let that = this
        if (!that.data.pileCodeStr) {
            that.toast.showToast('请输入编号');
            return
        }
        that.setData({
            isShow_Modal1: true
        })
    },
    //下线桩位
    offlinePile: function () {
        let that = this
        if (that.data.pileOffType == "其他") {
            that.toast.showToast('请输入备注');
            return
        }
        let reason = that.data.pileOffType
        that.hideModal1()
        lbbService.updatePileStatBatch({
            pileCode: that.data.pileCodeStr,
            nowStat: 2,
            reason: reason
        }, function (res) {
            that.toast.showToast('下线成功', 'success');
            that.pushPile('上海互邦', that.data.pileInfoList, '下线');
            console.log('下线触发事件')
            that.getEquipListInfo()
        })
    },
    hideModal2: function () {
        let that = this
        that.setData({
            isShow_Modal2: false,
            equipOffType: 'rfid卡损坏下线'
        })
    },
    hideModal3: function () {
        let that = this
        that.setData({
            isShow_Modal3: false,
            equipOffType: 'rfid卡损坏下线'
        })
    },
    //下线轮椅
    showModal2: function () {
        let that = this
        if (!that.data.chairCodeStr) {
            that.toast.showToast('请输入编号');
            return
        }
        that.setData({
            isShow_Modal2: true
        })
    },
    //下线担架
    showModal3: function () {
        let that = this
        if (!that.data.carCodeStr) {
            that.toast.showToast('请输入编号');
            return
        }
        that.setData({
            isShow_Modal3: true
        })
    },
    //下线轮椅/担架
    offlineEquip: function () {
        let that = this
        if (that.data.equipOffType == "其他") {
            that.toast.showToast('请输入备注');
            return
        }
        let reason = that.data.equipOffType
        let equipCodeStr = that.data.chairCodeStr;
        if (that.data.tabNum == 3 || that.data.tabNum == 4) {
            equipCodeStr = that.data.carCodeStr;
            that.hideModal3()
        } else {
            that.hideModal2()
        }
        lbbService.updateEquStatBatch({
            equId: equipCodeStr,
            nowStat: 2,
            reason: reason,
            equipType: that.data.equipType
        }, function (res) {
            that.toast.showToast('下线成功', 'success');
            that.pushEquip(equipCodeStr, '下线');
            that.getEquipListInfo()
        })
    },
    pushPile: function (hospitalName, pileList, status, equipType, location) {
        let that = this;
        let operatedPileListInfo = that.data.operatedPileListInfo;
        operatedPileListInfo.push({
            hospitalName: hospitalName,
            equipType: equipType,
            location: location,
            pileList: pileList,
            status: status
        })
        that.setData({
            operatedPileListInfo: operatedPileListInfo
        })
    },
    /**
     * 记录操作过的轮椅/担架
     */
    pushEquip: function (str, status) {
        let that = this
        let equipList = [];
        let operatedEquipListInfo = that.data.operatedChairListInfo;
        if (that.data.tabNum == 3) { //担架
            operatedEquipListInfo = that.data.operatedCarListInfo;
        }
        var newStr = str.replace(/\d+/g, function () {
            equipList.push({
                equipCode: arguments[0],
                eqtModelName: status == '上线' ? that.data.equip_ver : '--'
            });
        })
        operatedEquipListInfo.push({
            hospitalName: status == '上线' ? that.data.changedLocationInfo.hospitalName : '上海互邦',
            equipList: equipList,
            status: status
        });
        if (that.data.tabNum == 3) { //担架
            that.setData({
                operatedCarListInfo: operatedEquipListInfo
            })
        } else {
            that.setData({
                operatedChairListInfo: operatedEquipListInfo
            })
        }

    },
    scanBoxCode: function () {
        let that = this;
        wx.scanCode({
            success(res) {
                var qrc = '';
                if (res.result.indexOf(util.CONST_DATA.DEVICE_QRCODE_URL_PREFIX) == 0) {
                    var rst = res.result.split('/');
                    qrc = rst[rst.length - 1];
                    that.data.scan_qrc = qrc;
                } else {
                    var str = res.result.split(':')
                    var strn = ''
                    str.forEach(item => {
                        return strn += item
                    })
                    that.data.scan_qrc = strn;
                }
                lbbService.getMacId({
                    markCode: that.data.scan_qrc,
                    system: getApp().globalData.systemInfo ? getApp().globalData.systemInfo.system : 'ios'
                }, function (res) {
                    if (res.macId) {
                        that.setData({
                            gatewayHid: res.pileId,
                            gatewayMac: res.macId
                        })
                        console.log("scan tabNum-->", that.data.tabNum, that.data.gatewayHid)
                        that.getPileListInfo();
                    } else {
                        wx.showToast({
                            title: '获取mac失败',
                        })
                    }
                })
            },
            fail(res) {
                that.data.scanning = false;
            }
        })



    },

    //去录入网关设备信息
    toUpdateGateway: function (e) {
        let that = this
        let type = e.currentTarget.dataset.type
        that.data.gatewayTypeBtns.forEach(el => {
            if (el.type == type) {
                el.isActive = true
            } else {
                el.isActive = false
            }
        })
        that.data.gatewayType = type
        if (type == '4G') {} else if (type == 'NB') {}
        that.setData({
            gatewayTypeBtns: that.data.gatewayTypeBtns
        })
    },
    //下拉展开
    Exhibition:function(){
        let that =this
        if(that.data.typelistnum!=that.data.equipTypes.length){
            this.setData({
                typelistnum:that.data.equipTypes.length  
            })
            that.toggleAnimation(180)
            this.setData({
                type_animationData: this.animation.export()
              })
        }else{
            this.setData({
                typelistnum:3 
            })
            that.toggleAnimation(0)
            this.setData({
                type_animationData: this.animation.export()
              })
        }
    },
    //创建动画
    toggleAnimation: function(angle) {
        var animation = wx.createAnimation({
          timingFunction: "ease"
        })
        this.animation = animation;
        animation.rotate(angle).step()
      },
})